package com.koron.sinoprobe.web.engineering.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.bean.query.PageQuery;
import com.koron.common.core.interceptor.OrderByInterceptor;
import com.koron.sinoprobe.config.Constants;
import com.koron.sinoprobe.web.engineering.bean.po.SpDesign;
import com.koron.sinoprobe.web.engineering.bean.po.SpEstablish;
import com.koron.sinoprobe.web.engineering.bean.query.SpDesignQuery;
import com.koron.sinoprobe.web.engineering.bean.vo.SpDesignVO;
import com.koron.sinoprobe.web.engineering.mapper.SpDesignMapper;
import com.koron.sinoprobe.web.engineering.service.SpDesignService;
import com.koron.sinoprobe.web.engineering.service.SpEstablishService;
import com.koron.sinoprobe.web.ledger.bean.entity.SpProject;
import com.koron.sinoprobe.web.ledger.service.SpProjectService;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 项目设计(SpDesign)表服务实现类
 *
 * @author jzp
 * @since 2021-11-30 10:06:50
 */
@Service
public class SpDesignServiceImpl implements SpDesignService {

    @Autowired
    private SpEstablishService spEstablishService;

    @Autowired
    private SpProjectService spProjectService;

    /**
     * 通过ID查询单条数据
     */
    @Override
    @TaskAnnotation("getById")
    public SpDesignVO getById(SessionFactory factory, String id) {
        SpDesignMapper mapper = factory.getMapper(SpDesignMapper.class);
        SpDesignVO spDesignVO = mapper.selectById(id);
        SpEstablish spEstablish = spEstablishService.getByProjectId(factory, spDesignVO.getProjectId());
        Assert.notNull(spEstablish,"该项目【" + spDesignVO.getProjectName() + "】未立项，请先进行项目立项！");
        spDesignVO.setProjectCategory(spEstablish.getProjectCategory());
        spDesignVO.setSpEstablish(spEstablish);
        SpProject spProject = spProjectService.getById(factory, spDesignVO.getProjectId());
        spDesignVO.setSpProject(spProject);
        return spDesignVO;
    }

    /**
     * 查询所有数据
     */
    @Override
    @TaskAnnotation("list")
    public List<SpDesignVO> list(SessionFactory factory, SpDesignQuery query) {
        SpDesignMapper mapper = factory.getMapper(SpDesignMapper.class);
        return mapper.selectList(query);
    }

    @Override
    @TaskAnnotation("page")
    public PageInfo<SpDesignVO> page(SessionFactory factory, PageQuery pageQuery, SpDesignQuery query) {
        OrderByInterceptor.setOrderBy(pageQuery.getOrders());            // 设置排序，若不设置排序，插件会因为没有排序字段而直接跳过，不会影响分页插件
        PageHelper.startPage(pageQuery.getPage(), pageQuery.getPageSize());  //设置分页
        List<SpDesignVO> spDesignList = list(factory, query);
        return new PageInfo<>(spDesignList);
    }

    /**
     * 新增数据
     */
    @Override
    @TaskAnnotation("saveOrUpdate")
    public boolean saveOrUpdate(SessionFactory factory, SpDesign spDesign) {
        SpEstablish spEstablish = spEstablishService.getByProjectId(factory, spDesign.getProjectId());
        Assert.notNull(spEstablish,"请先进行项目立项！");
        // 默认草稿
        if (StringUtils.isEmpty(spDesign.getStatus())) {
            spDesign.setStatus(Constants.AppceptStatus.DRAFT);
        }
        SpDesignMapper mapper = factory.getMapper(SpDesignMapper.class);
        return mapper.insertOrUpdate(spDesign) > 0;
    }

    @Override
    @TaskAnnotation("end")
    public boolean end(SessionFactory factory, String id) {
        // 1. 更新状态
        return updateStatus(factory, id, Constants.AppceptStatus.APPROVED);
    }

    @Override
    @TaskAnnotation("submit")
    public boolean submit(SessionFactory factory, SpDesign spDesign) {
        // 2. 保存数据
        spDesign.setStatus(Constants.AppceptStatus.WAIT_REVIEW);
        saveOrUpdate(factory, spDesign);

        return true;
    }

    /**
     * 修改数据
     */
    @Override
    @TaskAnnotation("update")
    public boolean update(SessionFactory factory, SpDesign spDesign) {
        SpDesignMapper mapper = factory.getMapper(SpDesignMapper.class);
        return mapper.update(spDesign) > 0;
    }

    @Override
    @TaskAnnotation("updateStatus")
    public boolean updateStatus(SessionFactory factory, String id, String status) {
        SpDesign spDesign = new SpDesign();
        spDesign.setId(id);
        spDesign.setStatus(status);
        return update(factory, spDesign);
    }

    /**
     * 通过主键删除数据
     */
    @Override
    @TaskAnnotation("deleteById")
    public boolean deleteById(SessionFactory factory, String id) {
        SpDesign source = getById(factory, id);
        Assert.state(source.getStatus().equals(Constants.AppceptStatus.DRAFT), "非草稿状态不允许删除");
        SpDesignMapper mapper = factory.getMapper(SpDesignMapper.class);
        return mapper.hidden(id) > 0;
    }
}
